package com.hust.service;

import com.github.pagehelper.PageInfo;
import com.hust.dao.SubscribeDao;
import com.hust.dao.SubscribeExample;
import com.hust.domain.Subscribe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SubscribeService {

    @Autowired
    private SubscribeDao subscribeDao;

    //订阅功能
    public boolean addSubscribe(int uId,String uEmail,Long c_id,String sub_price){
        //避免重复订阅
        if (isSubscribe(uId,c_id)){
            return false;
        }
        //准备subscribe
        Subscribe record=new Subscribe();
        record.setuId(uId);
        record.setuEmail(uEmail);
        record.setcId(c_id.intValue());
        record.setSubPrice(sub_price);
        //添加记录
        try {
            subscribeDao.insert(record);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //查询用户是否订阅了某个商品
    public boolean isSubscribe(int uId,Long c_id){
        //准备subscribeExample
        SubscribeExample is_subscribeExample=new SubscribeExample();
        SubscribeExample.Criteria is_criteria=is_subscribeExample.createCriteria();
        is_criteria.andUIdEqualTo(uId);
        is_criteria.andCIdEqualTo(c_id.intValue());
        List<Subscribe> subscribes = subscribeDao.selectByExample(is_subscribeExample);
        if(subscribes.size()==0||subscribes==null){
            return false;
        }
        return true;
    }

    public List<Subscribe> scanAllSubscribes(){
        //准备subscribeExample
        SubscribeExample subscribeExample=new SubscribeExample();
        List<Subscribe> subscribes = subscribeDao.selectByExample(subscribeExample);
        return subscribes;
    }

}
